import { defineStore } from 'pinia'
import * as datascreenApi from '@/api/modules/datascreen'

export const useDatascreenStore = defineStore('datascreen', {
  state: () => ({
    overviewData: null,
    customerRiskData: null,
    regionData: null,
    realtimeAlerts: [],
    featureImportance: null,
    growthTrend: null,
    lifecycleData: null,
    currentUser: null,
    systemStats: null,
    loading: false
  }),

  actions: {
    // 获取概览数据
    async fetchOverviewData() {
      try {
        this.loading = true
        console.log('正在获取概览数据...')
        const response = await datascreenApi.getOverviewData()
        console.log('概览数据响应:', response)
        
        if (response.success) {
          this.overviewData = response.data
          console.log('概览数据设置成功:', this.overviewData)
          return response.data
        } else {
          throw new Error(response.message || '获取数据失败')
        }
      } catch (error) {
        console.error('获取概览数据失败:', error)
        // 返回模拟数据作为降级方案
        this.overviewData = this.getMockOverviewData()
        return this.overviewData
      } finally {
        this.loading = false
      }
    },

    // 获取客户风险分布数据
    async fetchCustomerRiskData() {
      try {
        console.log('正在获取客户风险分布数据...')
        const response = await datascreenApi.getCustomerRiskDistribution()
        console.log('客户风险分布数据响应:', response)
        
        if (response.success) {
          this.customerRiskData = response.data
          return response.data
        } else {
          throw new Error(response.message || '获取数据失败')
        }
      } catch (error) {
        console.error('获取客户风险分布数据失败:', error)
        this.customerRiskData = this.getMockCustomerRiskData()
        return this.customerRiskData
      }
    },

    // 获取实时预警数据
    async fetchRealtimeAlerts() {
      try {
        console.log('正在获取实时预警数据...')
        const response = await datascreenApi.getRealtimeAlerts()
        console.log('实时预警数据响应:', response)
        
        if (response.success) {
          this.realtimeAlerts = response.data
          return response.data
        } else {
          throw new Error(response.message || '获取数据失败')
        }
      } catch (error) {
        console.error('获取实时预警数据失败:', error)
        this.realtimeAlerts = this.getMockRealtimeAlerts()
        return this.realtimeAlerts
      }
    },

    // 获取当前用户信息
    async fetchCurrentUserInfo() {
      try {
        console.log('正在获取当前用户信息...')
        const response = await datascreenApi.getCurrentUserInfo()
        console.log('用户信息响应:', response)
        
        if (response.success) {
          this.currentUser = response.data
          return response.data
        } else {
          throw new Error(response.message || '获取数据失败')
        }
      } catch (error) {
        console.error('获取用户信息失败:', error)
        this.currentUser = {
          username: 'admin',
          fullName: '系统管理员',
          avatar: null,
          role: '管理员'
        }
        return this.currentUser
      }
    },

    // 模拟数据方法
    getMockOverviewData() {
      return {
        totalCustomers: 45678,
        customerGrowth: 3.2,
        highRiskCount: 3452,
        highRiskGrowth: 5.6,
        mediumRiskCount: 8712,
        mediumRiskGrowth: 2.1,
        lowRiskCount: 33514,
        lowRiskGrowth: -1.2,
        dailyStats: {
          predictCount: 12453,
          highRiskCount: 842,
          retainCount: 356
        },
        updateTime: new Date().toISOString()
      }
    },

    getMockCustomerRiskData() {
      return [
        { value: 3452, name: '高风险', itemStyle: { color: '#ff4757' } },
        { value: 8712, name: '中风险', itemStyle: { color: '#ffa502' } },
        { value: 33514, name: '低风险', itemStyle: { color: '#2ed573' } }
      ]
    },

    getMockRealtimeAlerts() {
      return [
        { 
          id: 1,
          time: '10:25:36', 
          level: 'high', 
          customerId: 'C10086',
          score: 92.5,
          content: '客户ID C10086 流失风险评分达到92.5，建议紧急干预' 
        },
        { 
          id: 2,
          time: '10:20:15', 
          level: 'medium', 
          customerId: 'C10087',
          score: 75.2,
          content: '客户ID C10087 流失风险评分达到75.2，建议关注' 
        }
      ]
    }
  }
})